package com.yjf.ljr.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yjf.ljr.R;
import com.yjf.ljr.adapter.CommonAdapter;
import com.yjf.ljr.adapter.CommonAdapter.HandleCallBack;
import com.yjf.ljr.common.CommonData;
import com.yjf.ljr.dataclass.IdentityInfoDataClass;
import com.yjf.ljr.dataclass.RechargDataClass;
import com.yjf.ljr.dataclass.IdentityInfoDataClass.IdentityInfo;
import com.yjf.ljr.dataclass.RechargDataClass.RechargeInfo;
import com.yjf.ljr.dataclass.WithdrawDataClass;
import com.yjf.ljr.parser.RequestBuilder.RequestObject;
import com.yjf.ljr.util.AppUtil;
import com.yjf.ljr.util.PicLoad;
import com.yjf.ljr.util.WithdrawDialog;

public class WithdrawActivity extends IjActivity implements OnClickListener {

	@ID("btWithdraw")
	private Button mBtWithdraw;
	@ID("etWithdrawMoney")
	private EditText mEtWithdrawMoney;
	@ID("withdraw_available_amount")
	private TextView availableAmount;
	
	private final int REQUEST_WITHDRAW_CODE = 1;

	private String availableBalance="";
	private String withdrawAmount;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_withdraw);		
		initControl();
	}

	private void initControl() {
		setLeftBtnClick();
		setTitleStr("申请提现");
		
		availableBalance = getIntent().getStringExtra("availableBalance");		
		availableAmount.setText(getString(R.string.red_packet_amount, availableBalance));
		mBtWithdraw.setOnClickListener(this);	
	}
	
	@Override
	public void onClick(View v) {
		withdrawAmount = mEtWithdrawMoney.getText().toString().trim();
		switch (v.getId()) {
		case R.id.btWithdraw:
			if (TextUtils.isEmpty(withdrawAmount)) {
				showToast("提现金额不能为空！");
				return;
			} else {
				double withDraw = AppUtil.moneyToNum(withdrawAmount);
//				if(withDraw <= 0) {
//					showToast("提现金额需大于0！");
//					return;
//				}
				
				if (!TextUtils.isEmpty(availableBalance)){
					double total = AppUtil.moneyToNum(availableBalance);
					if(withDraw > total) {
						showToast("提现金额不能大于账户可用余额！");
						return;
					}
				}
				new WithdrawTask(withdrawAmount).execute();
			}
			break;
		default:
			break;
		}
		
	}
		
	class WithdrawTask extends AsyncTask<Void, Void, String> {
		private WithdrawDataClass dc = new WithdrawDataClass();
        private String amount;		
		public WithdrawTask(String amount) {
			this.amount = amount;
		}
		
		@Override
		protected void onPreExecute() {
			showProgressDialog();
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Void... params) {
			RequestObject mObject = new RequestObject();
			mObject.method = "withraw";
			mObject.map.put("amount", amount);
			return requestDataAndGetErrorMsg(mObject, dc);
		}

		@Override
		protected void onPostExecute(String result) {
			dismissProgressDialog();
			if ("-1".equals(dc.code)) {
				loginWebview();
				return;
			}
			if (TextUtils.isEmpty(result)) {
				if(!TextUtils.isEmpty(dc.url)) {
					Intent intent = new Intent(WithdrawActivity.this,WvWithdrawActivity.class);
					intent.putExtra("url", dc.url);
					intent.putExtra("title", "申请提现");
					intent.putExtra("returnStr", "withdrawals/withdrawResult");
					startActivityForResult(intent, REQUEST_WITHDRAW_CODE);
				}
			} else {
				showToast(result);
			}
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if(requestCode == CommonData.REQUEST_CODE_LOGIN) {
			finish();
		}
		if(resultCode == RESULT_OK) {
			switch(requestCode) {
			case REQUEST_WITHDRAW_CODE:
				setResult(RESULT_OK);
				finish();
				break;
			default:
				break;
			}
		}
	}
}
